import 'package:efood_multivendor/controller/splash_controller.dart';
import 'package:efood_multivendor/data/api/api_checker.dart';
import 'package:efood_multivendor/data/model/response/basic_campaign_model.dart';
import 'package:efood_multivendor/data/model/response/product_model.dart';
import 'package:efood_multivendor/data/repository/campaign_repo.dart';
import 'package:get/get.dart';

class CampaignController extends GetxController implements GetxService {
  final CampaignRepo campaignRepo;
  CampaignController({required this.campaignRepo});

  List<BasicCampaignModel> _basicCampaignList = [];
  BasicCampaignModel? _campaign;
  List<Product> _itemCampaignList = [];
  List<AddOns> _addonList = [];
  List<int> _addOnQtyList = [];
  List<double> _addOnPriceList = [];
  String _selectedFilter = Get.find<SplashController>()
          .configModel
          ?.defaultCampaignFilter
          .toString() ??
      '';

  List<BasicCampaignModel> get basicCampaignList => _basicCampaignList;
  BasicCampaignModel? get campaign => _campaign;
  List<Product> get itemCampaignList => _itemCampaignList;
  List<AddOns> get addonList => _addonList;
  List<int> get addOnQtyList => _addOnQtyList;
  List<double> get addOnPriceList => _addOnPriceList;
  String get selectedFilter => _selectedFilter;

  Future<void> getBasicCampaignList(bool reload) async {
    if (_basicCampaignList.isEmpty || reload) {
      Response response = await campaignRepo.getBasicCampaignList();
      if (response.statusCode == 200) {
        _basicCampaignList = [];
        response.body.forEach((campaign) =>
            _basicCampaignList.add(BasicCampaignModel.fromJson(campaign)));
      } else {
        ApiChecker.checkApi(response);
      }
      update();
    }
  }

  Future<void> getBasicCampaignDetails(int campaignID) async {
    _campaign = null;
    Response response =
        await campaignRepo.getCampaignDetails(campaignID.toString());
    if (response.statusCode == 200) {
      _campaign = BasicCampaignModel.fromJson(response.body);
    } else {
      ApiChecker.checkApi(response);
    }
    update();
  }

  Future<void> getItemCampaignList(bool reload, {String filter = ''}) async {
    if (filter ==
        Get.find<SplashController>()
            .configModel!
            .defaultCampaignFilter
            .toString()) {
      clearFilters();
    }
    if (_itemCampaignList.isEmpty || reload) {
      Response response = await campaignRepo.getItemCampaignList(filter.isEmpty
          ? Get.find<SplashController>()
              .configModel!
              .defaultCampaignFilter
              .toString()
          : filter);
      if (response.statusCode == 200) {
        _itemCampaignList = [];
        response.body.forEach(
            (campaign) => _itemCampaignList.add(Product.fromJson(campaign)));
      } else {
        ApiChecker.checkApi(response);
      }
      update();
    }
  }

  void clearFilters() {
    _selectedFilter = Get.find<SplashController>()
        .configModel!
        .defaultCampaignFilter
        .toString();
    update();
  }

  void addAddon(AddOns addOn) {
    addonList.add(addOn);
    update();
  }

  void removeAddon(AddOns addOn, int index) {
    addonList.remove(addOn);
    update();
  }

  void setAddOnQuantity(bool isIncrement, int index) {
    if (isIncrement) {
      _addOnQtyList[index] = _addOnQtyList[index] + 1;
      update();
    } else {
      _addOnQtyList[index] = _addOnQtyList[index] - 1;
      update();
    }
  }

  void setSelectedFilter(String filter) {
    _selectedFilter = filter;
    update();
  }

  void setAddOnPrice(
      double price, int index, bool isFirstTap, bool isIncrement) {
    if (isFirstTap) {
      _addOnPriceList[index] = price;
    } else if (isIncrement) {
      _addOnPriceList[index] += price;
    } else {
      _addOnPriceList[index] -= price;
    }
  }

  void updateUi() {
    update();
  }
}
